// pages/search/index.js
import { request } from "../../request/index.js"
Page({
  data: {
    goods: [],
    // 取消 按钮 是否隐藏
    isFocus: false,
    // 输入框的值
    inpValue: ""
  },
  TimeId: -1,
  // 输入框的值发生改变 触发
  handleInput(e) {
    const { value } = e.detail
    // 检测合法性
    if (!value.trim()) {
      this.setData({
        isFocus: false,
        goods: []
      })
      clearTimeout(this.TimeId)
      return
    }
    // 准备发送请求获取数据
    this.setData({
      isFocus: true
    })
    clearTimeout(this.TimeId)
    this.TimeId = setTimeout(() => {
      this.qsearch(value)
    }, 1000);
  },
  // 发送请求获取搜索建议 数据
  async qsearch(query) {
    const res = await request({
      url: "/goods/search", data: { query }
    })
    console.log(res.goods)
    this.setData({
      goods: res.goods
    })
  },
  // 点击 取消按钮
  handleCancel () {
    this.setData({
      inpValue: "",
      isFocus: false,
      goods: []
    })
  }
})